import Reflux from 'reflux'
import actions from './actions'

//给数组添加remove方法，用于去除指定下标的元素
// Array.prototype.remove=function(dx)
// {
//     if(isNaN(dx)||dx>this.length){return false;}
//     for(var i=0,n=0;i<this.length;i++)
//     {
//         if(this[i]!=this[dx])
//         {
//             this[n++]=this[i]
//         }
//     }
//     this.length-=1
// };

export default Reflux.createStore({
    addressList: [],
    addressItem: {},

    //监听所有的actions
    listenables: [actions],
    //on开头的都是action触发后的回调函数
    onGetAddressList () {
        //更新状态（就是个对象）
        util.request("customer/shopping_address",{},"GET",(data)=>{
            this.addressList = data.data;
            this.trigger({addressList: this.addressList});
        });


    },
    onSetDefault(index){
        util.request("customer/default_address",{
            shoppingAddressId : this.addressList[index].id
        },"POST" ,(data)=>{
            this.addressList.map((item,i)=>{
               item.default = false;
               if(index === i){
                   item.default = true;
               }
            });
            this.trigger({addressList: this.addressList})
        });
        // this.addressList.map((item,i)=>{
        //     item.isDef = false;
        //     if(index === i){
        //         item.isDef = true;
        //     }
        // });

        // this.trigger({addressList: this.addressList});
    },

    //编辑-保存操作项
    onEditAddress(index){
        this.addressList.map((item,i)=>{
            if(index === i){
                this.addressItem = item
            }
        });
    },

    //获取操作项
    onGetAddressItem(callback){
        //更新状态（就是个对象）
        this.trigger({
            addressItem: this.addressItem,
            active: this.addressItem.default,
        },()=>{
            callback()
        })
    },

    onAddAddress(item){
        util.request("customer/address_create",{
            shoppingMan: item.name,
            shoppingManPhone: item.phone,
            locationId: item.address,
            address: item.place,
            default: item.isDef,
        },"POST",function (q) {
            console.log(q)
        });
        // this.addressList.push(item);
        this.trigger({addressList: this.addressList});
    },
    onRemoveAddress(index){
        util.request("customer/delete_address",{
            shoppingAddressId:this.addressList[index].id
        },"POST");
        this.addressList.map((item,i)=>{
            if(index === i){
                this.addressList.splice(index,1);
            }
        });
        this.trigger({addressList:this.addressList});
    },
    onSaveChange(obj,index){
        util.request("customer/address_update",{
            shoppingMan: obj.name,
            shoppingManPhone: obj.phone,
            locationId: obj.address,
            address: obj.place,
            default: obj.isDef,
            id: obj.id
        },"POST");
        // this.addressList[index] =obj;
    }
    // onSaveChange(obj,index){
    //     this.addressList.map((item,i)=>{
    //         if (index === i){
    //             // this.addressList[index] = obj
    //             this.changedAddressItem = obj;
    //
    //         }
    //     });console.log(this.addressList[index])
    // }
});